﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BobcatMultiTouch;

namespace Sample3_DrawingWithMultipleInputAreas
{
    /// <summary>
    /// This sample shows how you can use the MultiTouchInputManager (MTIM) to get input
    /// into a number of different user controls hosted by the main form.
    /// </summary><remarks>
    /// This class does not do very much itself. It just initializes the MTIM when the form loads,
    /// and shows the MTIM's config when a button is pressed. The actual drawing is done in
    /// two instances of the DrawingControl user-control which are shown on the form.
    /// 
    /// The MTIM needs to be initialized close to the start of the program, and the main
    /// form-load event is a good place to do this. In this case we initialize it from config 
    /// stored in a file. 
    /// 
    /// The config file holds details of which input sources are available (e.g. mouse and
    /// wiimote), whether they are active and any special config for them. A typical config
    /// is included with this project (MultiTouchConfig.xml) and is set to copy to the output
    /// folder when the project is built. See this wiki page for more details about the XML
    /// config format:
    /// http://code.google.com/p/bobcat-wii-lib/wiki/MTIMXMLConfig
    /// </remarks>
    public partial class Form1 : Form
    {
        #region Public methods

        /// <summary>
        /// Constructor.
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Called when the form is first loaded. We initialize the MTIM from config data
        /// stored in a file.
        /// </summary>
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // We intialize the MTIM...
                MultiTouchInputManager.getInstance().initializeFromFile(CONFIG_FILENAME);
            }
            catch (Exception ex)
            {
                // The MTIM can throw exceptions in initializeFromFile if one or more sources cannot be activated...
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Called just before the form closes. We close the MTIM. This isn't 100% necessary, but it's
        /// good practice.
        /// </summary>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MultiTouchInputManager.getInstance().close();
        }

        /// <summary>
        /// Called when the "Show config..." button is pressed. We show the MTIM's config screen
        /// and then store any updated config.
        /// </summary>
        private void cmdShowConfig_Click(object sender, EventArgs e)
        {
            MultiTouchInputManager.getInstance().showConfigScreen();
            MultiTouchInputManager.getInstance().saveToFile(CONFIG_FILENAME);
        }

        #endregion

        #region Private data

        // File that stores XML config about which sources are used, wiimote calibration etc...
        private const string CONFIG_FILENAME = "MultiTouchConfig.xml";

        #endregion


    }
}
